package org.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import org.example.springboot.common.Result;
import org.example.springboot.entity.User;
import org.example.springboot.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 功能：
 * 作者：扫钢
 * 日期：2024/11/13 12:12
 */
@CrossOrigin
@RestController
public class WebController {

    @Resource
    UserService userService;
    @GetMapping("/")
    public Result hello() {
        return Result.success();
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            return Result.error("数据输入不合法");
        }
        user = userService.login(user);
        return Result.success(user);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())){
            return Result.error("数据输入不合法");
        }
        if(user.getUsername().length() > 10 || user.getPassword().length() > 20){
            return Result.error("数据输入不合法");
        }
        userService.register(user);
        return Result.success("注册成功");
    }
}